package com.ln.yygh.cmn.controller;

import com.ln.yygh.cmn.Dict;
import com.ln.yygh.cmn.service.DictService;
import com.ln.yygh.common.result.Result;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author: lianXiaoChen
 * @create: 2021-05-25 15:21
 */
@Api(description = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Autowired
    private DictService dictService;
    //根据dictCode查询下级节点
    @ApiOperation("根据数据id查询子数据列表")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode){
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }

    //根据数据id查询子数据列表
    @ApiOperation(value = "根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable Long id) {
        List<Dict> list = dictService.findChlidData(id);
        return Result.ok(list);
    }

    //导出数据字典接口
    @GetMapping("/exporData")
    @ApiOperation(value = "导出数据字典接口")
    public void exporData(HttpServletResponse response) {
        dictService.exporDiceData(response);
    }

    //导入数据字典信息
    @PostMapping("/importData")
    public Result importData(MultipartFile file) {
        dictService.importData(file);
        return Result.ok();
    }

    //根据dictcode和value查询
    @GetMapping("/getName/{dictCode}/{value}")
    @ApiOperation(value = "根据用户dictcode和value去数据字典中查询")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value) {
        return dictService.getDictName(dictCode, value);
    }

    //根据value查询
    @GetMapping("/getName/{value}")
    @ApiOperation(value = "根据用户的value去数据字典中查询")
    public String getName(@PathVariable String value) {
        return dictService.getDictName("", value);
    }
}
